(function($) {	
  $.modalbox=function(theme, label) {
	  if(label){
		  var l = label.length;
		  var s = $.modalbox.settings;
		  s.confirmTitle = l>0?label[0]:s.confirmTitle;
		  s.infoTitle    = l>1?label[1]:s.infoTitle;
		  s.warnTitle    = l>2?label[2]:s.warnTitle;
		  s.errTitle     = l>3?label[3]:s.errTitle;	  
		  s.yesLabel     = l>4?label[4]:s.yesLabel;
		  s.noLabel      = l>5?label[5]:s.noLabel;
		  s.okLabel      = l>6?label[6]:s.okLabel;
	  }
	  if(theme != window.undefined && theme != $.modalbox.settings.theme) 
		  $.modalbox.init(theme);
	  $(window).bind('scroll', $.modalbox.center);
	  $(window).bind('resize', $.modalbox.center);
  }

  $.modalbox.settings={
    theme		:'',
    modalboxId	:'#modalbox',
    closeImg   	:'close.png',
    confirmTitle:'Confirm',
    infoTitle	:'Info',
    warnTitle	:'Warning',
    errTitle	:'Error',
    yesLabel	:'Yes',
    noLabel		:'No',
    okLabel		:'Ok',
    defHeight	:'300px',
  	html		:'\
  	<div class="popup"> \
      <table class="table"> \
        <tbody> \
          <tr> \
            <td class="shadow_tl"/><td class="shadow_b"/><td class="shadow_tr"/> \
          </tr> \
          <tr> \
            <td class="shadow_b"/> \
            <td class="body"> \
              <table class="header"> \
	      		 <tr> \
	      			<td style="text-align: left;"><span class="title"></span></td> \
	      			<td style="text-align: right;"><a href="#modalbox" class="close"><img src="'+this.closeImg+'" class="close_image"/></a></td> \
	      		 </tr> \
	      	  </table> \
              <div class="content"> \
              	<table width="100%" border="0" cellpadding="1" cellspacing="1"> \
              		<tr> \
              			<td width="10" align="left" valign="top"><img class="icon"/></td> \
              			<td style="text-align: left;" valign="top"><span class="message"></span></td> \
              		</tr> \
              	</table> \
              </div> \
              <div class="buttons"> \
        		<div class="no">'+this.noLabel+'</div> \
        		<div class="yes">'+this.yesLabel+'</div> \
        		<div class="ok">'+this.okLabel+'</div> \
        	  </div> \
            </td> \
            <td class="shadow_b"/> \
          </tr> \
          <tr> \
            <td class="shadow_bl"/><td class="shadow_b"/><td class="shadow_br"/> \
          </tr> \
        </tbody> \
      </table> \
    </div>'
  }

  $.modalbox.loading = function() {
	  var pageScroll = $.wis.common.getPageScroll();
	  var pageSize   = $.wis.common.getPageSize();
	  $($.modalbox.settings.modalboxId).css({top: pageScroll[1],left: pageScroll[0],width: pageSize[0],height: pageSize[1]}).show();
	  $.modalbox.center();
	  $(document).bind('keydown.modalbox', function(e) {
		  if (e.keyCode == 27) $.modalbox.close()
	  });
  }
  
  $.modalbox.center = function() {
	  var s = $.modalbox.settings;
	  var pHeight = $(s.modalboxId+' .popup').height();
	  var pWidth  = $(s.modalboxId+' .popup').width();
	  var pageScroll = $.wis.common.getPageScroll();
	  var pageSize   = $.wis.common.getPageSize();
	  var pTop = Math.round(45*(pageSize[1]-pHeight)/pageSize[1]);
	  if(pTop<0)
		  pTop = 0;
	  $(s.modalboxId).css({top: pageScroll[1], left: pageScroll[0], width: pageSize[0], height: pageSize[1]});
	  $(s.modalboxId+' .popup').css({top: pTop+'%'});	  
  }

  $.modalbox.close = function() {	
	  $(document).trigger('close.modalbox');
	  return false
  }

  $(document).bind('close.modalbox', function() {
	  var s = $.modalbox.settings;
	  $(document).unbind('keydown.modalbox')
	  $(s.modalboxId).fadeOut($.modalbox.init(s.theme));
  })
  
  $.modalbox.show = function(type,title,msg,theme){
	  var s = $.modalbox.settings;
	  if(theme != window.undefined && theme != s.theme) 
		  $.modalbox.init(theme);
	  $.modalbox.loading(true);
	  if(type=='confirm'){
		  $(s.modalboxId+' .ok').css({display: 'none'});
		  $(s.modalboxId+' .yes').css({display: 'block'});
		  $(s.modalboxId+' .no').css({display: 'block'});		  
	  }else{
		  $(s.modalboxId+' .ok').css({display: 'block'});
		  $(s.modalboxId+' .yes').css({display: 'none'});
		  $(s.modalboxId+' .no').css({display: 'none'});
	  }
	  $(s.modalboxId+' .title').text(title);
	  $(s.modalboxId+' .icon').attr("src", $.modalbox.settings.theme+'/'+type+'.png');
	  $(s.modalboxId+' .message').html(msg);
	  var h  = $(s.modalboxId+' .content').height();
	  var dh = 0; 
	  if(s.defHeight.substring(s.defHeight.length-2,s.defHeight.length)=='px')
		  dh = eval(s.defHeight.substring(0,s.defHeight.length-2));
	  if(s.defHeight.substring(s.defHeight.length-1,s.defHeight.length)=='%')
		  dh = eval(s.defHeight.substring(0,s.defHeight.length-2))*$.wis.common.getPageSize()[1];
	  if(h>dh)
		  $(s.modalboxId+' .content').css({height: dh});
  }
  
  $.modalbox.showConfirm = function(msg,val,cb){
	  var s = $.modalbox.settings;
	  $.modalbox.show('confirm',s.confirmTitle,msg);
	  $(s.modalboxId+' .yes').click(function(){
			$.modalbox.close();
			if($.isFunction(cb)) cb(val);
	  });
  }
  
  $.modalbox.showError = function(msg){
	$.modalbox.show('error',$.modalbox.settings.errTitle,msg);
	if(msg.indexOf(errorMsgTimeout) != -1){
		top.location.href = sessionTimeoutRedirectUrl;
	}
  }
  
  $.modalbox.showInfo = function(msg){
	  $.modalbox.show('info',$.modalbox.settings.infoTitle,msg);
  }
  
  $.modalbox.showWarning = function(msg){
	  $.modalbox.show('warning',$.modalbox.settings.warnTitle,msg);
  }
  
  $.modalbox.init = function(theme) {
	  	var s = $.modalbox.settings;
		s.theme = theme;
		$(s.modalboxId).remove();
		$('body').append('<div id="'+s.modalboxId.substring(1,s.modalboxId.length)+'" style="display:none;">'+s.html+'</div>');	    
	    var preload = [];
	    $(s.modalboxId).find('.b:first, .bl, .br, .tl, .tr').each(function() {
	      preload.push(new Image());
	      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1');
	    });
	    $(s.modalboxId+' .close').click($.modalbox.close);
	  	$(s.modalboxId+' .close_image').attr('src', s.theme+'/'+s.closeImg);
	    $(s.modalboxId+' .yes').text(s.yesLabel);	    
		$(s.modalboxId+' .no').text(s.noLabel);
		$(s.modalboxId+' .no').click($.modalbox.close);
		$(s.modalboxId+' .ok').text(s.okLabel);
		$(s.modalboxId+' .ok').click($.modalbox.close);
		var cssHeight = $(s.modalboxId+ ' .content').css('height');
		$(s.modalboxId+' .content').css({height: 'auto'});
	  	if(cssHeight && cssHeight!='')
	  		s.defHeight = cssHeight;
  }
})(jQuery);
